package Gui;

import Silnik.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
 *
 * @author Jakub Banaszewski
 */
public class WyborWalut extends JPanel implements ActionListener
{

   public void uruchomPrzycisk()
   {
      wykonaj.setEnabled(true);
   }

   //<editor-fold defaultstate="collapsed" desc="MyCellRenderer">
   private static class MyCellRenderer extends BasicComboBoxRenderer
   {

      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
      {
         if (isSelected)
         {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
            if (-1 < index)
               list.setToolTipText(Kursy.getCounty(value.toString()));
         }
         else
         {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
         }
         setFont(list.getFont());
         setText((value == null) ? "" : value.toString());
         return this;
      }
   }
   //</editor-fold>
   private JComboBox waluta;
   private JSpinner zakresOd, zakresDo;
   private JButton wykonaj;
   private final ReceivingListener sluchacz;

   public WyborWalut(String[] wal, ReceivingListener sluchacz)
   {
      this.sluchacz = sluchacz;

      this.setSize(new Dimension(200, 600));
      this.setBorder(BorderFactory.createCompoundBorder(
              BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
              BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)));
      if (wal != null)
         waluta = new JComboBox(wal);
      else
         waluta = new JComboBox(Kursy.getSkróty().toArray());

      waluta.setRenderer(new MyCellRenderer());

      ArrayList daty = new ArrayList(Kursy.getDaty());
      Collections.sort(daty);
      
      zakresOd = new JSpinner(new SpinnerListModel(daty));
      zakresOd.setValue(daty.get(daty.size()-2));
      zakresDo = new JSpinner(new SpinnerListModel(daty));
      zakresDo.setValue(daty.get(daty.size()-1));

      wykonaj = new JButton("Rysuj wykres");
      wykonaj.setActionCommand("rysuj");
      wykonaj.addActionListener(this);
   }

   //<editor-fold defaultstate="collapsed" desc="Inicjacja Gui">
   public void initGui()
   {
      GroupLayout layout = new GroupLayout(this);
      setLayout(layout);
      layout.setAutoCreateGaps(true);

      JLabel labelOd = new JLabel("Data rozpoczęcia");
      JLabel labelDo = new JLabel("Data zakończenia");

      labelOd.setLabelFor(zakresOd);
      labelDo.setLabelFor(zakresDo);

      GroupLayout.ParallelGroup htop = layout.createParallelGroup();
      htop.addComponent(waluta);
      GroupLayout.ParallelGroup hbotton = layout.createParallelGroup();

      GroupLayout.SequentialGroup h1 = layout.createSequentialGroup();
      h1.addComponent(labelOd);
      h1.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
      h1.addComponent(zakresOd);

      GroupLayout.SequentialGroup h2 = layout.createSequentialGroup();
      h2.addComponent(labelDo);
      h2.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
      h2.addComponent(zakresDo);

      hbotton.addGroup(h1);
      hbotton.addGroup(h2);
      hbotton.addComponent(wykonaj);

      htop.addGroup(hbotton);


      layout.setHorizontalGroup(htop);


      GroupLayout.SequentialGroup vtop = layout.createSequentialGroup();
      GroupLayout.SequentialGroup vbotton = layout.createSequentialGroup();

      vtop.addComponent(waluta, 20, GroupLayout.PREFERRED_SIZE, 50);
      vtop.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
      GroupLayout.ParallelGroup v1 = layout.createParallelGroup();

      v1.addComponent(labelOd, 20, GroupLayout.PREFERRED_SIZE, 50);
      v1.addComponent(zakresOd, 20, GroupLayout.PREFERRED_SIZE, 50);

      GroupLayout.ParallelGroup v2 = layout.createParallelGroup();
      v2.addComponent(labelDo, 20, GroupLayout.PREFERRED_SIZE, 50);
      v2.addComponent(zakresDo, 20, GroupLayout.PREFERRED_SIZE, 50);

      vbotton.addGroup(v1);
      vbotton.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
      vbotton.addGroup(v2);
      vbotton.addComponent(wykonaj);

      vtop.addGroup(vbotton);

      layout.setVerticalGroup(vtop);

      this.setVisible(true);
   }
   //</editor-fold>

   @Override
   public void actionPerformed(ActionEvent e)
   {
      JButton przycisk = (JButton) e.getSource();
      MyDate poczatek = (MyDate) zakresOd.getValue();
      MyDate koniec = (MyDate) zakresDo.getValue();
      
      if (poczatek.after(koniec))
      {
         JOptionPane.showMessageDialog(this, "Podano nieprawidłową datę!");
         zakresOd.setValue(zakresDo.getValue());
      }
      else
      {
         przycisk.setEnabled(false);
         PobieraczKursu pk = new PobieraczKursu(sluchacz, waluta.getSelectedItem().toString());
         pk.setStartDate(poczatek);
         pk.setEndDate(koniec);
         pk.start();
      }
   }
}
